package com.launcher.wkang.adapter;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.launcher.wkang.App;
import com.launcher.wkang.R;
import com.launcher.wkang.bean.AppInfo;

import java.util.ArrayList;
import java.util.List;

public class ApplicationAdapter extends BaseAdapter {
    private List<ResolveInfo>appList;
    private Context mContact;
    private LayoutInflater mLayoutInflator;
    private List<String> noShowList;
    public  ApplicationAdapter(Context context){
        mContact=context;
        mLayoutInflator=LayoutInflater.from(context);
        noShowList=new ArrayList<String>();
        noShowList.add("com.launcher.wkang");
        noShowList.add("cn.rongcloud.im");
        noShowList.add("xu.ye");
        noShowList.add("io.github.mthli.Ninja");
        noShowList.add("com.baidu.input");
        noShowList.add("com.android.settings");
    }
    @Override
    public int getCount() {
        return appList==null?0:appList.size();
    }
    public void initData(List<ResolveInfo> initAppList){
        if (this.appList==null){
            this.appList=new ArrayList<ResolveInfo>();
        }
        if (initAppList!=null&&initAppList.size()>0){
            this.appList.clear();
            for (int j=0;j<initAppList.size();j++){
                ResolveInfo resolveInfo=initAppList.get(j);
                String pkg = resolveInfo.activityInfo.packageName;
//                去除不需要显示的应用图标
                if (!noShowList.contains(pkg)){
                    appList.add(resolveInfo);

                }
            }

        }

        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return appList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView=mLayoutInflator.inflate(R.layout.item_app,null);
            viewHolder=new ViewHolder();
            viewHolder.appIcon=(ImageView) convertView.findViewById(R.id.app_icon);
            viewHolder.appName=(TextView) convertView.findViewById(R.id.app_name);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        ResolveInfo info = appList.get(position);
        if (App.iconMap.containsKey(info.activityInfo.packageName)){
            viewHolder.appIcon.setImageResource(App.iconMap.get(info.activityInfo.packageName));
        }else {
            viewHolder.appIcon.setImageDrawable(info.activityInfo.loadIcon(mContact.getPackageManager()));
        }
//        Glide.with(mContact).load(info.getAppIcon()).into(viewHolder.appIcon);
        viewHolder.appName.setText(info.activityInfo.loadLabel(mContact.getPackageManager()));
        return convertView;
    }
    private class ViewHolder{
        private ImageView appIcon;
        private TextView appName;
    }
}
